package com.my.ads.lib.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;

import com.my.ads.lib.Config;
import com.my.ads.lib.activity.PowerActivity;

import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;

/**
 * Created by xuanyang on 2018/3/7.
 */

public class PowerReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(Config.TagLog, "power");

        SharedPreferences sps =context. getSharedPreferences("POWER_CONNECTED", Context.MODE_PRIVATE);
        Long showInterName = sps.getLong("Power", 0L);

        if (Math.abs(System.currentTimeMillis() - showInterName) > 2000){
            SharedPreferences sp = context.getSharedPreferences("POWER_CONNECTED", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sp.edit();

            editor.putLong("Power", System.currentTimeMillis());
            editor.apply();

            Intent tmp = new Intent(context, PowerActivity.class);
            tmp.setFlags(FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(tmp);
        }


    }
}
